package com.tech.service.impl;

import java.util.List;
import javax.annotation.Resource;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageInfo;
import com.tech.params.ItemParams;
import com.tech.pojo.TbItem;
import com.tech.pojo.TbItemDesc;
import com.tech.service.ItemService;
import com.tech.service.TbItemDescService;
import com.tech.util.BeanCopierUtils;
import tk.mybatis.mapper.entity.Example;


/**
 * 商品相关
 */
@Service // dubbo服务
@org.springframework.stereotype.Service
public class ItemServiceImpl extends IServiceImpl<TbItem>  implements ItemService{

    @Resource(name = "TbItemDescServiceImpl")
    private TbItemDescService itemDescServiceImpl;
	
	@Override
	public TbItem getItemById(long itemId) {
		TbItem item = this.selectByPrimaryKey(itemId);
		return item;
	}

	
    @Override
    public PageInfo<TbItem> getItemList(int pageNum, int pageSize) {
        //设置分页信息
        List<TbItem> list = this.selectByExampleByPage(TbItem.class, pageNum, pageSize);
        PageInfo<TbItem> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }


    @Override
    public boolean add(ItemParams params) {
        params.initItem();
        TbItem item = new TbItem();
        BeanCopierUtils.copyProperties(params, item);
        this.insertSelective(item);
        // 创建一个商品描述表对应的pojo
        itemDescServiceImpl.insertSelective(params.initItemDesc(params));
        return true;
    }


    @Override
    public void updateItem(ItemParams params) {
        TbItem record = new TbItem();
        // 更新item内容
        BeanCopierUtils.copyProperties(params,record);
        this.updateByPrimaryKeySelective(record);
        // 更新desc内容
        Example example = new Example(TbItemDesc.class);
        //example.createCriteria().andEqualTo("itemId", params.getId());
        TbItemDesc itemDesc = new TbItemDesc();
        itemDesc.setItemDesc(params.getItemDesc());
        itemDescServiceImpl.updateByExample(itemDesc, example);
    }

    @Override
    public void updateItemByStatus(long itemId,int status) {
        TbItem record = new TbItem();
        record.setId(itemId);
        record.setStatus((byte)status);
        this.updateByPrimaryKeySelective(record);
    }
}
